var webpack = require('webpack'),
    HtmlWebpackPlugin = require('html-webpack-plugin'), //模板插件
    ExtractTextPlugin = require("extract-text-webpack-plugin"), //Css分割插件
    CleanWebpackPlugin = require('clean-webpack-plugin'), // 删除插件
    NgAnnotatePlugin = require('ng-annotate-webpack-plugin'), //自动注入注解插件
    OpenBrowserWebpackPlugin=require('open-browser-webpack-plugin'), //自动打开浏览器插件
    FastUglifyJsPlugin = require('fast-uglifyjs-plugin'), //快速压缩插件
    autoprefixer = require('autoprefixer'),//自动添加兼容前缀插件
    path = require('path'),
    buildPath = path.resolve(__dirname, "build"), //发布目录
    __DEV__ = process.env.NODE_ENV === 'dev', //发布环境
    publicPath = '', //资源引用统一前缀
    devtool = '', //source-map模式

    plugins = [
        new HtmlWebpackPlugin({
            chunks: ['app', 'vendor'],
            template: __dirname + '/template/index.html',
            filename: './index.html'
        }),
        new webpack.optimize.CommonsChunkPlugin({name:'vendor', filename:'script/vendor.[hash:6].js'}),
        new CleanWebpackPlugin('build', {
            verbose: true,	//将log写到 console.
            dry: false	//不要删除任何东西，主要用于测试.
        }),
        new ExtractTextPlugin("css/styles.[hash:6].css"),
        new NgAnnotatePlugin({
            add: true
        }),
         new webpack.LoaderOptionsPlugin({  
            options: {  
                postcss: function(){  
                    return [  
                        require("autoprefixer")({  
                            browsers: ['ie>=8','>1% in CN']  
                        })  
                    ]  
                },
                babel: {
                    babelrc: false,
                    presets: [
                        ["es2015", {"modules": false}]
                    ],
                } 
            }  
        })  
    ];

    function assetsPath(_path) {
      return path.posix.join('static', _path)
    }

if (!__DEV__) {
    //压缩
    plugins.push(new webpack.optimize.UglifyJsPlugin({ //代码压缩 
        comments: true, //显示注释 
        mangle: false, //取消代码混淆 
        compress: {
            warnings: true //在UglifyJs删除没有用到的代码时不输出警告 
        }
    }));

    publicPath = process.env.NODE_ENV == 'test' ? 'test.domain.com' : 'www.domain.com';
    devtool = 'source-map';
}else{
    plugins.push(new OpenBrowserWebpackPlugin({url:'http://localhost:5555'}));
}

var config = {
    //入口文件配置
    entry: {
        app: path.resolve(__dirname, 'app/app.js'),
        vendor: ["angular", 'angular-ui-router', 'oclazyload']
    },
    //文件导出的配置
    output: {
        path: buildPath,
        filename: "script/[name].[hash:6].js",
        // publicPath: publicPath,
        chunkFilename: "chunks/[name].chunk.[chunkhash].js"
    },
    //本地服务器配置
    devServer: {
        historyApiFallback: true,
        hot: true,
        inline: true
    },
    //模块配置
    module: {
        rules: [
          {
            test: /\.js[x]?$/,
            exclude: "/node_modules/",
            use: {
               loader: 'babel-loader?cacheDirectory=true'
            },
          },
          {
            test: /\.html$/,
            loader: 'raw-loader'
          },
          {
            test: /\.(scss|sass|css)$/,
            loader: ExtractTextPlugin.extract({fallback: "style-loader", use: "css-loader!postcss-loader!sass-loader"}),
            exclude: "/node_modules/"
          },
          {
            test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
            loader: require.resolve('url-loader'),
            options: {
              limit: 10000,
              name: assetsPath('img/[name].[hash:7].[ext]')
            },
            exclude: "/node_module/"
          },
          {
            test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
            loader: 'url-loader',
            options: {
              limit: 10000,
              name: assetsPath('fonts/[name].[hash:7].[ext]')
            }
          },
          {
            test: require.resolve('jquery'),
            use: [{
              loader: 'expose-loader',
              options: 'jQuery'
            }, {
              loader: 'expose-loader',
              options: '$'
            }]
          }
        ]
    },


    
    //插件配置
    plugins: plugins,
    //调试配置
    devtool: devtool
}

module.exports = config;
